package cn.shyouchi.wuguanjia.android.api.attendance;

import android.content.Context;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.shyouchi.wuguanjia.android.api.ClientProvider;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.ui.attendance.AttendanceEntity;
import cn.shyouchi.wuguanjia.android.ui.attendance.report.StaffPostEntity;
import cn.shyouchi.wuguanjia.android.ui.attendance.report.StaffReportEntity;
import cn.shyouchi.wuguanjia.android.ui.attendance.signin.AttendanceInfoEntity;
import cn.shyouchi.wuguanjia.android.ui.attendance.signout.SignbackRank;
import cn.shyouchi.wuguanjia.android.ui.attendance.statistics.StatisticsInfoEntity;
import cn.shyouchi.wuguanjia.android.ui.attendance.temporary.TemporaryTaskEntity;
import cn.shyouchi.wuguanjia.android.ui.reportstatistic.ReportDayStatisticEntity;
import cn.shyouchi.wuguanjia.android.ui.reportstatistic.ReportStatisticEntity;
import cn.shyouchi.wuguanjia.android.ui.statistic.NewlyInfo;
import cn.shyouchi.wuguanjia.android.ui.statistic.SignStatisticManagerEntity;
import cn.shyouchi.wuguanjia.android.ui.statistic.UserMonthRank;
import cn.shyouchi.wuguanjia.android.ui.statistic.page.MonthStatisticEntity;
import cn.shyouchi.wuguanjia.android.ui.statistic.page.YearStatisticEntity;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.AbseUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.EarlyUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.LateUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.OutUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.PracticeUser;
import cn.shyouchi.wuguanjia.android.ui.statistic.user.SecondedUser;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;
import top.zibin.luban.Luban;

/**
 * Created by Eenie on 2016/12/14 at 10:37
 * Email: 472279981@qq.com
 * Des:考勤模块的API
 */

public class AttendanceApi {
    private AttendanceService mService;
    private Context mContext;


    public AttendanceApi(Context context) {
        UserStorage mUserStorage = UserStorageProvider.Default();
        mContext = context;
        Retrofit retrofit = ClientProvider.getDefaultClient().getRetrofit(mUserStorage);
        mService = retrofit.create(AttendanceService.class);
    }


    public Observable<AttendanceEntity> getUserSchedulingList(String date) {
        return mService.getUserSchedulingList(date);
    }

    public Observable<AttendanceInfoEntity> getCheckAttendance() {
        return mService.getCheckAttendance();
    }


    public Observable<BaseRequestResult> isLast(String id) {
        return mService.isLast(id);
    }

    public Observable<SignStatisticManagerEntity> getEvents() {
        return mService.getEvents();
    }

    public Observable<YearStatisticEntity> getStatisticDate() {
        return mService.getStatisticDate();
    }

    public Observable<MonthStatisticEntity> getStatisticData(String date, String projectId) {
        return mService.getStatisticData(date, projectId);
    }

    public Observable<MonthStatisticEntity> getYearStatisticData(String date, String projectId) {

        return mService.getYearStatisticData(date, projectId);
    }

    public Observable<BaseResult<SignbackRank>> getSignbackRank() {
        return mService.getSignbackRank();
    }


    public Observable<BaseRequestResult<List<UserMonthRank>>> getMothRankStatisticData(String date, boolean isYear, String projectid) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothRankStatisticData(path, date, projectid);
    }

    public Observable<BaseRequestResult<List<UserMonthRank>>> getIntegratedStatisticData(String date, boolean isYear, String projectid) {

        String path = "month";
        if (isYear) {
            path = "year";
        }

        return mService.getIntegratedStatisticData(path, date, projectid);
    }


    public Observable<BaseRequestResult<List<UserMonthRank>>> getMothRefueStatisticData(String date, boolean isYear, String projectid) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothRefueStatisticData(path, date, projectid);
    }

    public Observable<BaseRequestResult<List<NewlyInfo>>> getMothNewlyStaff(String date, boolean isYear, String projectid) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothNewlyStaff(path, date, projectid);
    }

    public Observable<BaseRequestResult<List<LateUser>>> getMothLateUsers(String date, boolean isYear, String projectId) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothLateUsers(path, date, projectId);
    }

    public Observable<BaseRequestResult<List<EarlyUser>>> getMothEarlyUsers(String date, boolean isYear, String projectId) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothEarlyUsers(path, date, projectId);
    }

    public Observable<BaseRequestResult<List<AbseUser>>> getMothAbseUserUsers(String date, boolean isYear, String projectId) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothAbseUserUsers(path, date, projectId);
    }

    public Observable<BaseRequestResult<List<OutUser>>> getMothOutUserUsers(String date, boolean isYear, String projectid) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothOutUserUsers(path, date, projectid);
    }

    public Observable<BaseRequestResult<List<SecondedUser>>> getMothSecondedUserUsers(String date, boolean isYear, String projectid) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothSecondedUserUsers(path, date, projectid);
    }

    public Observable<BaseRequestResult<List<PracticeUser>>> getMothPracticeUserUsers(String date, boolean isYear, String projectid) {
        String path = "month";
        if (isYear) {
            path = "year";
        }
        return mService.getMothPracticeUserUsers(path, date, projectid);
    }


    public Observable<BaseRequestResult> signIn(final Map<String, RequestBody> paramsMap, String path) {


        File file = new File(path);
        return Luban.get(mContext)
                .load(file)
                .putGear(Luban.THIRD_GEAR)
                .asObservable()
                .flatMap(new Func1<File, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(File file) {
                        MultipartBody.Part body = null;
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
                        return mService.signIn(paramsMap, body);
                    }
                });
    }

    public Observable<BaseRequestResult> signOut(final Map<String, RequestBody> paramsMap, String path) {

//        final Map<String, RequestBody> paramsMap = new HashMap<>();
//        final RequestBody projectsIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), serviceid);
//        paramsMap.put("serviceid", projectsIdBody);
//        final RequestBody IdBody = RequestBody.create(MediaType.parse("multipart/form-data"), type);
//        paramsMap.put("type", IdBody);
//        final RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), latitude);
//        paramsMap.put("latitude", nameBody);
//        final RequestBody ageBody = RequestBody.create(MediaType.parse("multipart/form-data"), longitude);
//        paramsMap.put("longitude", ageBody);
//        final RequestBody heightBody = RequestBody.create(MediaType.parse("multipart/form-data"), address);
//        paramsMap.put("address", heightBody);

        File file = new File(path);
        return Luban.get(mContext)
                .load(file)
                .putGear(Luban.THIRD_GEAR)
                .asObservable()
                .flatMap(new Func1<File, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(File file) {
                        MultipartBody.Part body = null;
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
                        return mService.signOut(paramsMap, body);
                    }
                });


    }


    public Observable<BaseRequestResult> temporarySignIn(String proinsuranceid,
                                                         String type,
                                                         String address,
                                                         String path) {

        final Map<String, RequestBody> paramsMap = new HashMap<>();
        final RequestBody projectsIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), proinsuranceid);
        paramsMap.put("proinsuranceid", projectsIdBody);
        final RequestBody IdBody = RequestBody.create(MediaType.parse("multipart/form-data"), type);
        paramsMap.put("type", IdBody);
        final RequestBody heightBody = RequestBody.create(MediaType.parse("multipart/form-data"), address);
        paramsMap.put("address", heightBody);

        File file = new File(path);
        return Luban.get(mContext)
                .load(file)
                .putGear(Luban.THIRD_GEAR)
                .asObservable()
                .flatMap(new Func1<File, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(File file) {
                        MultipartBody.Part body = null;
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
                        return mService.temporarySignIn(paramsMap, body);
                    }
                });


    }


    public Observable<TemporaryTaskEntity> getTemporaryTask() {
        return mService.getTemporaryTask();
    }


    public Observable<StatisticsInfoEntity> getStatistics() {
        return mService.getStatistics();
    }

    public Observable<StatisticsInfoEntity> getStatistics(String userId, String date) {
        return mService.getStatisticsByUser(userId, date);
    }


    public Observable<StaffPostEntity> getSignPostList() {
        return mService.getSignPostList();
    }

    public Observable<StaffReportEntity> getSignPostDetail(String postId) {
        return mService.getSignPostDetail(postId);
    }

    public Observable<ReportStatisticEntity> getReportStatistic(String id) {
        return mService.getReportStatistic(id);
    }

    public Observable<BaseRequestResult<List<ReportDayStatisticEntity>>> getReportStatisticInfo(String date, String projectId) {

        return mService.getReportStatisticInfo(date, projectId);
    }


    public Observable<BaseRequestResult> reportAdd(String postsettingid,
                                                   String id,
                                                   String time,
                                                   String completed,
                                                   String path) {

        final Map<String, RequestBody> paramsMap = new HashMap<>();
        final RequestBody projectsIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), postsettingid);
        paramsMap.put("postsettingid", projectsIdBody);
        final RequestBody IdBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);
        paramsMap.put("id", IdBody);
        final RequestBody heightBody = RequestBody.create(MediaType.parse("multipart/form-data"), time);
        paramsMap.put("time", heightBody);
        final RequestBody completedBody = RequestBody.create(MediaType.parse("multipart/form-data"), completed);
        paramsMap.put("completed", completedBody);
        File file = new File(path);
        return Luban.get(mContext)
                .load(file)
                .putGear(Luban.THIRD_GEAR)
                .asObservable()
                .flatMap(new Func1<File, Observable<BaseRequestResult>>() {
                    @Override
                    public Observable<BaseRequestResult> call(File file) {
                        MultipartBody.Part body = null;
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
                        return mService.reportAdd(paramsMap, body);
                    }
                });


    }


}
